//
//  CYSettingsSwitchCell.m
//  cyjf
//
//  Created by tauCross on 2017/8/22.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYSettingsSwitchCell.h"



@implementation CYSettingsSwitchCellObject

@end



@interface CYSettingsSwitchCell ()

@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UISwitch *cellSwitch;
@property(nonatomic, strong)UIImageView *bottomLine;

@property(nonatomic, strong)CYSettingsSwitchCellObject *object;

@end


@implementation CYSettingsSwitchCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        self.titleLabel.textColor = HEXCOLOR(@"444444");
        [self.contentView addSubview:self.titleLabel];
        
        self.cellSwitch = [[UISwitch alloc] init];
        [self.cellSwitch addTarget:self action:@selector(onCellSwitchValueChanged) forControlEvents:UIControlEventValueChanged];
        self.cellSwitch.onTintColor = APP_DEFAULT_COLOR_BLUE;
        [self.contentView addSubview:self.cellSwitch];

        self.bottomLine = [[UIImageView alloc] initWithImage:[UIImage imageWithColor:HEXCOLOR(@"ECECEC") size:CGSizeMake(1, 1)]];
        [self.contentView addSubview:self.bottomLine];
    }
    return self;
}

+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 50;
}

- (BOOL)shouldUpdateCellWithObject:(id)object
{
    self.object = object;
    
    self.titleLabel.text = self.object.title;
    
    self.cellSwitch.on = self.object.on;
    
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.titleLabel sizeToFit];
    self.titleLabel.left = 20;
    self.titleLabel.centerY = self.contentView.height / 2;

    self.cellSwitch.centerY = self.contentView.height / 2;
    self.cellSwitch.right = self.contentView.width - 20;
    
    self.bottomLine.width = self.contentView.width - 20;
    self.bottomLine.height = 1 / SCREEN_SCALE;
    self.bottomLine.left = 20;
    self.bottomLine.bottom = self.contentView.height;
}

#pragma mark - Control Events
- (void)onCellSwitchValueChanged
{
    if(self.object.switchAtion)
    {
        self.object.switchAtion(self.cellSwitch.on);
    }
}

@end
